import grails.converters.XML
class ProviderBuyController {

    def index = { }

    static allowedMethods = [findByName:'POST']

    def requestBuy = {
        println params
        def marketRegistry = MarketRegistry.get(1)
        def url = marketRegistry.fullAddress+"/market/buy/"+params.code
        def post = new Post("url":url)
        def xmlResponse = post.getText()
        def xml = new XmlSlurper().parseText(xmlResponse)
        println "ProviderBuyController-requestBuy ("+url+") "+xmlResponse
        xml.each { p ->
            def currentProduct = new Product()
            currentProduct.name = p.name.text()
            currentProduct.code = p.code.text()
            currentProduct.address = "http://"+InetAddress.getLocalHost().getHostAddress()+":"+request.getLocalPort()+request.getContextPath()
            currentProduct.price = Double.parseDouble(p.price.text())
            currentProduct.delivery = Integer.parseInt(p.delivery.text())
            currentProduct.available = Boolean.parseBoolean(p.available.text())
            currentProduct.save()
        }
        redirect(controller:"product",action:"list")
    }

    def buy = {
        def p = Product.findByCode(params.code)
        def xml = "<status>false<status>"
        if(p != null) {
            xml = p as XML
            p.delete()
        }
        println("ProviderBuyController-buy"+xml)
        render xml
    }

    def findByName = {
        def marketRegistry = MarketRegistry.get(1)
        def url = marketRegistry.fullAddress+"/market/xml/product/findByName/"+params.name
        def get = new Get("url":url)
        def xmlResponse = get.getText()
        def products = new ArrayList()
        if(xmlResponse.contains("<?xml version")) {
            def xml = new XmlSlurper().parseText(xmlResponse)
             println "ProviderBuyController-findByName ("+url+") "+xmlResponse
            xml.product.each { p ->
                def value = LocalConfigurationRegistry.get(1).getValue();
                if(!p.address.text().equals(value)) {
                    def currentProduct = new Product()
                    currentProduct.name = p.name.text()
                    currentProduct.code = p.code.text()
                    currentProduct.address = p.address.text()
                    currentProduct.price = Double.parseDouble(p.price.text())
                    currentProduct.delivery = Integer.parseInt(p.delivery.text())
                    currentProduct.available = Boolean.parseBoolean(p.available.text())
                    products.add(currentProduct)
                }
            }
        }
        [ productInstanceList: products ]
    }

    def searchByName = {
        def p = []
        println "ProviderBuyController-searchByName ("+params.name+")"
        if(params.name != null && !params.name.equals("null") && !params.name.equals(""))
            p = Product.findAllByNameLikeAndAvailable("%"+params.name+"%",true)
        else
            p = Product.findAllByAvailable(true)
        render p as XML
    }
}
